package demo.formater;

import general.InterfaceFormatter;

import java.util.Formatter;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringPattern implements InterfaceFormatter {
	private String message = "asd";
	@Override
	public void run() {
		String email = "a@asd.com.vn";
		if (isEmail(email)) {
			System.out.println("Day la dia chi email");
		} else {
			System.out.println(message);
		}

	}

	private boolean isEmail(String email) {
		boolean isEmail = true;
		Formatter formatter = new Formatter();
		System.out.println(formatter.format("%,20.3f", 42042042f).toString());
		
		StringTokenizer st = new StringTokenizer(email,".");
		while (st.hasMoreElements()) {
			System.out.println(st.nextToken());;
			
		}
		Pattern pt = Pattern.compile("^[.]+|^[@]+");
		Matcher ma = pt.matcher(email);
		if (ma.matches()) {
			message = "Khong duoc co ky tu . hoac @ o dau";
			isEmail = false;
		}
		pt = Pattern.compile("\\.");
		String[] split = pt.split(email);
		for (int i = 0; i < split.length; i++) {
			String part = split[i];
			if (i == 0) {
				pt = Pattern.compile("^[a-zA-Z0-9]+@+[a-zA-Z0-9]$");
				ma = pt.matcher(part);
				if (!ma.find()) {
					message = "Khong dung";
					return false;
				}
			}
		}
		
		return isEmail;
	}

}
